package com.example.xiaoshuo_yudu.controller;


import com.example.xiaoshuo_yudu.config.SecurityConfig;
import com.example.xiaoshuo_yudu.entity.Result;
import com.example.xiaoshuo_yudu.entity.dto.BookmarkDto;
import com.example.xiaoshuo_yudu.service.BookshelfService;
import com.example.xiaoshuo_yudu.service.UsersService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.security.Security;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户书架表 前端控制器
 * </p>
 *
 * @author xusen
 * @since 2025-07-31
 */
@RestController
@RequestMapping("/api/users")
public class BookshelfController {
    @Autowired
       private UsersService usersService;
    @Autowired
    private BookshelfService bookshelfService;

    @PostMapping("/bookmarks")
    public Result<String> addBookmark(
            @Valid @RequestBody BookmarkDto request){
//        从安全框架获取用户id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = null;
        if(authentication != null&& authentication.getPrincipal() instanceof Long){
            userId = (Long) authentication.getPrincipal();
        }
        if(userId==null)
            return Result.failure(401,"请先登录");
       bookshelfService.addBookmark(userId, request);
        return Result.success("添加成功");
    }
    @GetMapping ("/getbookshelf")
    public Result<List<Map<String, Object>>> getBookshelf(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = null;
        if(authentication != null&& authentication.getPrincipal() instanceof Long){
            userId = (Long) authentication.getPrincipal();
        }
        if(userId==null)
            return Result.failure(401,"请先登录");
        List<Map<String, Object>> bookshelf = bookshelfService.getBookshelf(userId);
        return Result.success(bookshelf);
    }
    @GetMapping ("/reading-history")
    public Result<List<Map<String, Object>>> getBookreading(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = null;
        if(authentication != null&& authentication.getPrincipal() instanceof Long){
            userId = (Long) authentication.getPrincipal();
        }
        if(userId==null)
            return Result.failure(401,"请先登录");
        List<Map<String, Object>> bookshelf = bookshelfService.getBookreading(userId);
        return Result.success(bookshelf);
    }
    /**
     * 删除书架中的书籍（支持单个和批量删除）
     * @param ids 书籍ID列表
     * @return 操作结果
     */
    @PostMapping("/bookshelf/delete")
    public Result<String> deleteBooks(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.failure(400, "请选择要删除的书籍");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = null;
        if(authentication != null&& authentication.getPrincipal() instanceof Long){
            userId = (Long) authentication.getPrincipal();
        }
        if(userId==null)
            return Result.failure(401,"请先登录");
        boolean success = bookshelfService.deleteBooks(ids,userId);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.failure(5001,"删除失败，请重试");
        }
    }

    @PostMapping("/reading-history/delete")
    public Result<String> deleteReadingBooks(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.failure(400, "请选择要删除的书籍阅读记录");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = null;
        if(authentication != null&& authentication.getPrincipal() instanceof Long){
            userId = (Long) authentication.getPrincipal();
        }
        if(userId==null)
            return Result.failure(401,"请先登录");
        boolean success = bookshelfService.deleteReadingBooks(ids,userId);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.failure(5001,"删除失败，请重试");
        }
    }
}
